import { Controller, Delete, Get, Patch, Post } from '@nestjs/common';
import { UserService } from './user.service';
import { ConfigService } from '@nestjs/config';
import { ConfigEnum } from '../enum/config.enum';
import { User } from './user.entity';

@Controller('user')
export class UserController {
  constructor(
    private userService: UserService,
    private configService: ConfigService,
  ) {}
  @Get()
  getUsrers(): object {
    // const db = this.configService.get('db'); db是ymal文件中配置的key
    const db = this.configService.get(ConfigEnum.DB_TYPE);
    console.log(db);
    return this.userService.findAll();
  }

  @Post()
  addUser() {
    const user = {
      username: 'bugs',
      password: '123',
    };
    return this.userService.create(user as unknown as User);
  }

  @Patch()
  updateUser() {
    const user = { username: 'newName' } as unknown as User;
    return this.userService.update(1, user);
  }

  @Delete()
  deleteUser() {
    return this.userService.remove(1);
  }

  @Get('/profile')
  getProfile() {
    return this.userService.findProfile(2);
  }

  @Get('/logsByGroup')
  async getLogsByGroup() {
    const res = await this.userService.findLogsByGroup(2);
    return res.map((o) => ({
      result: o.result,
      count: o.count,
    }));
  }
}
